// ---------------------------------------------- Code Base 1 -----FWS1.java----------------------------------

// Second part of Controller

   public FWS()

   {

      DataBanker.init();

      MessageGenerator m = new MessageGenerator();

      m.start();
			
			for (int i = 0; i < sensors.length; i++){
	      
	      SensorMonitor s = new SensorMonitor(sensors[i]);
	
	      s.start();
    }

   }

}



class DataBanker

{

   public static void init()

   {
			for (int i = 0; i < FWS.sensors.length; i++){
	     	
	     	Vector v = new Vector();
	     	
	      for (int j = 0; j < FWS.HistoryLength; j++)
	
	         v.addElement(new SensorReading(1,0));
	     map.put(FWS.sensors[i].name, v);
	     }
	     
   }

   public static synchronized void write(String name, SensorReading r)

   {
			Vector v = map.get(name);
			
			if (v.size() > 0)
     	 	v.removeElementAt(0);

      v.addElement(new SensorReading(r.res,r.value));

   }

   public static synchronized Vector read(String name)

   {
		
      return (Vector)map.get(name).clone();

   }
	 
	 private static HashMap<String, Vector> map = new HashMap<String, Vector>();
  
}



class SensorMonitor extends Thread

{

		public Sensor sensor;
		
		public SensorMonitor(Sensor sensor){
				
				this.sensor = sensor;
				
		}

   public void run()

   {

      while (true) {

         try { Thread.sleep(sensor.period); }

         catch (InterruptedException e) {}

// For each sensor included in the family member, one sensor read and one data

// banker write will be appended here, followed by Code Base 2. 






